package com.newtouch.ainsurtech.batch.schedule;/**
 * Created by jovi on 26/09/2017.
 */
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import com.newtouch.ainsurtech.batch.job.demo.PersonJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2016
 * All rights reserved. 2017-09-26 18:48
 * </p>
 *
 * @author MaoJiaWei
 * @version 1.0
 */
@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private PersonJob personJob;


    @Scheduled(cron = "0/30 * * * * *")
    public void runJob() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
        // make unique JobParameters so now instance of job can be started
        Map<String, JobParameter> confMap = new HashMap<String, JobParameter>();
        confMap.put("time", new JobParameter(System.currentTimeMillis()));
        JobParameters jobParameters = new JobParameters(confMap);
        JobExecution ex = jobLauncher.run(personJob.demoJob(), jobParameters);
        log.info("Execution status----->" + ex.getStatus());
    }
}
